/*-
 * Copyright (c) 2009-2012 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ Bitmap: bitmap.c    Rev. 1.0.6    2012-10-12T07:59+08:00 $
 */

#include <stdlib.h>
#ifdef TRACE_INFO
#include <stdio.h>
#endif

#include <bscerr.h>
#include <bitmap.h>


int bitmap_init(bitmap_t *bm, size_t sz)
{
    int ierr = ERRNO_UNDEFINED;

    size_t szc;  /* Size candidate */
    size_t szr;  /* Remain size */

#ifdef TRACE_INFO
    printf("[F] bitmap_init()\n");
#endif
    bm->size = sz;

    /* Compute block size */
    szc = sz / BITMAP_BLOCK_SIZE;
    szr = sz % BITMAP_BLOCK_SIZE;

    if (szr == 0)
    {
        bm->bksz = szc;
    }
    else
    {
        bm->bksz = szc + 1;
    }

    bm->list = (uint8_t*) malloc(bm->bksz);

#ifndef NOERR_CHKS
    if (bm->list == NULL)
    {
        ierr = ERRNO_CANNOT_ALLOCATE_MEMORY;
    }
#endif

#ifdef TRACE_INFO
    printf("    bitmap_init() Passed!\n");
#endif

    return ierr;
}


int bitmap_dest(bitmap_t *bm)
{
    int ierr = ERRNO_UNDEFINED;

#ifdef TRACE_INFO
    printf("[F] bitmap_dest()\n");
#endif
    bm->bksz = 0;
    bm->size = 0;

    free(bm->list);

#ifdef TRACE_INFO
    printf("    bitmap_dest() Passed!\n");
#endif

    return ierr;
}


int bitmap_set_mark(bitmap_t *bm, size_t ps)
{
    int ierr = ERRNO_UNDEFINED;

    size_t bkps;  /* Block position */
    size_t rmps;  /* Remain position */

#ifdef TRACE_INFO
    printf("[F] bitmap_set_mark()\n");
#endif

    bkps = ps / BITMAP_BLOCK_SIZE;
    rmps = ps % BITMAP_BLOCK_SIZE;

    if (bkps > bm->bksz)
    {
        ierr = ERRNO_OUT_OF_RANGE_POSITION;
    }
    else
    {
        switch (rmps)
        {
            case 0:
                bm->list[bkps] |= BITMAP_MARK_0;
                break;
            case 1:
                bm->list[bkps] |= BITMAP_MARK_1;
                break;
            case 2:
                bm->list[bkps] |= BITMAP_MARK_2;
                break;
            case 3:
                bm->list[bkps] |= BITMAP_MARK_3;
                break;
            case 4:
                bm->list[bkps] |= BITMAP_MARK_4;
                break;
            case 5:
                bm->list[bkps] |= BITMAP_MARK_5;
                break;
            case 6:
                bm->list[bkps] |= BITMAP_MARK_6;
                break;
            case 7:
                bm->list[bkps] |= BITMAP_MARK_7;
                break;
        }
    }

#ifdef TRACE_INFO
    printf("    bitmap_set_mark() Passed!\n");
#endif

    return ierr;
}


int bitmap_set_none(bitmap_t *bm, size_t ps)
{
    int ierr = ERRNO_UNDEFINED;

    size_t bkps;  /* Block position */
    size_t rmps;  /* Remain position */

#ifdef TRACE_INFO
    printf("[F] bitmap_set_none()\n");
#endif

    bkps = ps / BITMAP_BLOCK_SIZE;
    rmps = ps % BITMAP_BLOCK_SIZE;

    if (bkps > bm->bksz)
    {
        ierr = ERRNO_OUT_OF_RANGE_POSITION;
    }
    else
    {
        switch (rmps)
        {
            case 0:
                bm->list[bkps] &= BITMAP_NONE_0;
                break;
            case 1:
                bm->list[bkps] &= BITMAP_NONE_1;
                break;
            case 2:
                bm->list[bkps] &= BITMAP_NONE_2;
                break;
            case 3:
                bm->list[bkps] &= BITMAP_NONE_3;
                break;
            case 4:
                bm->list[bkps] &= BITMAP_NONE_4;
                break;
            case 5:
                bm->list[bkps] &= BITMAP_NONE_5;
                break;
            case 6:
                bm->list[bkps] &= BITMAP_NONE_6;
                break;
            case 7:
                bm->list[bkps] &= BITMAP_NONE_7;
                break;
        }
    }

#ifdef TRACE_INFO
    printf("    bitmap_set_none() Passed!\n");
#endif

    return ierr;
}


int bitmap_check(bitmap_t *bm, size_t ps, int *bl)
{
    int ierr = ERRNO_UNDEFINED;

    size_t bkps;  /* Block position */
    size_t rmps;  /* Remain position */

#ifdef TRACE_INFO
    printf("[F] bitmap_check()\n");
#endif

    bkps = ps / BITMAP_BLOCK_SIZE;
    rmps = ps % BITMAP_BLOCK_SIZE;

    if (bkps > bm->bksz)
    {
        ierr = ERRNO_OUT_OF_RANGE_POSITION;
    }
    else
    {
        switch (rmps)
        {
            case 0:
                if ((bm->list[bkps] & BITMAP_MARK_0) == BITMAP_MARK_0)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
            case 1:
                if ((bm->list[bkps] & BITMAP_MARK_1) == BITMAP_MARK_1)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
            case 2:
                if ((bm->list[bkps] & BITMAP_MARK_2) == BITMAP_MARK_2)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
            case 3:
                if ((bm->list[bkps] & BITMAP_MARK_3) == BITMAP_MARK_3)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
            case 4:
                if ((bm->list[bkps] & BITMAP_MARK_4) == BITMAP_MARK_4)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
            case 5:
                if ((bm->list[bkps] & BITMAP_MARK_5) == BITMAP_MARK_5)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
            case 6:
                if ((bm->list[bkps] & BITMAP_MARK_6) == BITMAP_MARK_6)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
            case 7:
                if ((bm->list[bkps] & BITMAP_MARK_7) == BITMAP_MARK_7)
                {
                    *bl = BITMAP_BIT_MARK;
                }
                else
                {
                    *bl = BITMAP_BIT_NONE;
                }
                break;
        }
    }

#ifdef TRACE_INFO
    printf("    bitmap_check() Passed!\n");
#endif

    return ierr;
}


int bitmap_get_marks(bitmap_t *bm, size_t *mkz)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;  /* size_t based loop counter or index */

    int bl = BITMAP_BIT_NONE;

#ifdef TRACE_INFO
    printf("[F] bitmap_get_marks()\n");
#endif

    *mkz = 0;

    for (zi = 0; zi < bm->size; zi++)
    {
        ierr = bitmap_check(bm, zi, &bl);

        if (bl == BITMAP_BIT_MARK)
        {
            (*mkz)++;
        }
    }

#ifdef TRACE_INFO
    printf("    bitmap_get_marks() Passed!\n");
#endif

    return ierr;
}


int bitmap_get_nones(bitmap_t *bm, size_t *nnz)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;  /* size_t based loop counter or index */

    int bl = BITMAP_BIT_MARK;

#ifdef TRACE_INFO
    printf("[F] bitmap_get_nones()\n");
#endif

    *nnz = 0;

    for (zi = 0; zi < bm->size; zi++)
    {
        ierr = bitmap_check(bm, zi, &bl);

        if (bl == BITMAP_BIT_NONE)
        {
            (*nnz)++;
        }
    }

#ifdef TRACE_INFO
    printf("    bitmap_get_nones() Passed!\n");
#endif

    return ierr;
}


int bitmap_sync(bitmap_t *bma, bitmap_t *bmb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;  /* size_t based loop counter or index */

#ifdef TRACE_INFO
    printf("[F] bitmap_sync()\n");
#endif

    if (bma->size == bmb->size)
    {
        for (zi = 0; zi < bma->bksz; zi++)
        {
            bma->list[zi] = bma->list[zi] | bmb->list[zi];
            bmb->list[zi] = bma->list[zi];
        }
    }
    else
    {
        ierr = ERRNO_MODULES_DUMP_SIZE;
    }

#ifdef TRACE_INFO
    printf("    bitmap_sync() Passed!\n");
#endif

    return ierr;
}

